﻿using System;
using System.Diagnostics;

namespace LantinxShare.Common
{
    public class Ext : IExt
    {
        /// <summary>
        /// Executes presentation service code.
        /// </summary>
        /// <param name="action">A delegate to execute.</param>
        public void Execute(Action action)
        {
            Execute(() =>
            {
                action();
                return (object)null;
            }, null);
        }

        /// <summary>
        /// Executes presentation service code.
        /// </summary>
        /// <param name="action">A delegate to execute.</param>
        /// <param name="finalAction">When finally execute function.</param>
        public void Execute(Action action, Action finalAction)
        {
            Execute(() =>
            {
                action();
                return (object)null;
            }, finalAction);
        }

        /// <summary>
        /// Executes presentation service code.
        /// </summary>
        /// <typeparam name="TResult">The result type.</typeparam>
        /// <param name="action">A delegate to execute.</param>
        /// <param name="finalAction">When finally execute function.</param>
        /// <returns>Returns the result.</returns>
        public TResult Execute<TResult>(Func<TResult> action, Action finalAction)
        {
            TResult result = default(TResult);

            try
            {
                result = action();
            }
            catch(Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
            finally
            {
                if (finalAction != null)
                {
                    finalAction();
                }
            }

            return result;
        }


       
    }
}
